//
//  ViewController.m
//  Runtime
//
//  Created by jikuan zhang on 2022/6/7.
//  Copyright © 2022 hsgd. All rights reserved.
//

#import "ViewController.h"
#import "AQBody.h"
#import "AQPerson.h"
#import <objc/runtime.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    AQPerson *p = [[AQPerson alloc] init];
    const char * cName;

    cName =  object_getClassName(p);
    
    NSLog(@"%s",cName);

    Class cls = NSClassFromString([NSString stringWithFormat:@"%s", object_getClassName(p)]);
    
    
    id p1 = [[cls alloc] init];
    
    if ([p1 isKindOfClass: p.class]) {
        NSLog(@"破");
    }
    
    object_getIndexedIvars(p1);
    
    unsigned int methodCount = 0;
    Method *methods = class_copyMethodList(cls, &methodCount);
    if (methods) {
        NSMutableDictionary *methodInfos = [NSMutableDictionary new];
        for (unsigned int i = 0; i < methodCount; i++) {
            Method method = methods[i];
            SEL sel = method_getName(method);
            IMP imp = method_getImplementation(method);
            const char *name = sel_getName(sel);
            NSLog(@"m - %s",name);


        }
        free(methods);
    }
    
    
    unsigned int propertyCount = 0;
    objc_property_t *properties = class_copyPropertyList(cls, &propertyCount);
    if (properties) {
        NSMutableDictionary *propertyInfos = [NSMutableDictionary new];
        for (unsigned int i = 0; i < propertyCount; i++) {
            objc_property_t property = properties[i];
            const char *name = property_getName(property);
            if (name) {
                NSString *aName = [NSString stringWithUTF8String:name];
            }
            NSLog(@"p - %s",name);
        }
     

        free(properties);
    }
    
    unsigned int ivarCount = 0;
    Ivar *ivars = class_copyIvarList(cls, &ivarCount);
    if (ivars) {
        NSMutableDictionary *ivarInfos = [NSMutableDictionary new];
        for (unsigned int i = 0; i < ivarCount; i++) {
            Ivar ivar = ivars[i];
            const char *name = ivar_getName(ivar);
            NSLog(@"%s",name);
            ptrdiff_t offset = ivar_getOffset(ivar);
            NSLog(@"%ld",offset);
            const char *typeEncoding = ivar_getTypeEncoding(ivar);
            NSLog(@"%s",typeEncoding);

        }
        free(ivars);
    }
    

}


@end
